package cz.swosh.stomp.ejb;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.apache.shiro.crypto.hash.Sha512Hash;

/**
 * Implementace AppUserDAO. Má za ukol CRUD operace s User entitami.
 *@author
 */
@Stateless(name = "AppUserDAO")
@Remote(AppUserDAORemote.class)
@Local(AppUserDAO.class)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AppUserDAOBean implements AppUserDAO, AppUserDAORemote{
    @PersistenceContext(unitName = "stomp")
    private EntityManager em;

    private static final String salt = "maminkamojemila";
    protected Logger log = Logger.getLogger(getClass().getName());

    /**
     */
    public AppUser getAppUserEmail(String email) {
        try {
            return em.find(AppUser.class, email);
        } catch (Exception e) {
            log.log(Level.WARNING, e.getMessage());
            return new AppUser();
        }
    }
    /**
     * TODO uneused untest
     */
    public List<AppUser> getAllAppUsersWOHesloAktHash() {
        try {
            List<AppUser> listAppUser = new ArrayList<AppUser>();
            Query query = em.createNativeQuery("SELECT email FROM  " + (AppUser.class).getSimpleName());
            List list = query.getResultList();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object[] record = (Object[]) it.next();
                AppUser appUser = new AppUser((String)record[0]);
                listAppUser.add(appUser);
            }

            return listAppUser;
        } catch (Exception e) {
            log.log(Level.WARNING, e.getMessage());
            return new ArrayList<AppUser>();
        }
    }

    public boolean deleteAppUser(AppUser appUser) {
        boolean ok = true;
        try {
            em.remove(appUser);
            return ok;
        } catch (Exception e) {
            log.log(Level.WARNING, e.getMessage());
            ok = false;
            return ok;
        }
    }

    public boolean saveAppUser(AppUser appUser) {
        try {
            String email = appUser.getEmail();
            String password = appUser.getPassword();
            Hash passHash = new Sha512Hash(password, salt);
            String passHashHex = passHash.toHex();
            Hash aktHash = new Sha1Hash(email);
            String aktHashHex = aktHash.toHex();
            appUser.setAktHash(aktHashHex);
            AppUser appUserFromDB = getAppUserEmail(email);
            if (appUserFromDB == null || !email.equals(appUserFromDB.getEmail())) {
                em.persist(appUser);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.log(Level.WARNING, e.getMessage());
            return false;
        }
    }

    public String getAktHash(String email) {
        try {
            AppUser appUser = new AppUser();
            Query query = em.createNativeQuery("SELECT akt_hash FROM " + (AppUser.class).getSimpleName() + " WHERE email = :email ");
            query.setParameter("email", email);
            List list = query.getResultList();
            String aktHash = (String) list.get(0);
            return aktHash;
        } catch (Exception e) {
            log.log(Level.WARNING, e.getMessage());
            return "";
        }
    }

}
